package com.ziq.zcamp.util;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SpanUtils;

import java.math.RoundingMode;
import java.text.DecimalFormat;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class StringUtil {
    public static String formatCode(String original){
        return original.trim().replaceAll(" ", "").replaceAll("(.{4})", "$1 ").trim();
    }

    @SuppressLint("DefaultLocale")
    public static String formatCodeForWanUnit(int count){
        if(count > 10000){
            DecimalFormat decimal = new DecimalFormat("0.0");
            decimal.setRoundingMode(RoundingMode.DOWN);
            return decimal.format((count / 10000.0)) + "万";
        } else {
            return String.valueOf(count);
        }
    }

    /**
     * 设置textView结尾...后面显示的文字和颜色
     * @param textView textview
     * @param maxLines 最多的行数
     * @param originText 原文本
     * @param endText 结尾文字
     * @param endColor 结尾文字颜色
     * @param isExpand 当前是否是展开状态
     */
    public static void toggleEllipsize(
            final TextView textView,
            final int maxLines,
            final String originText,
            final String endText,
            final String endColor,
            final boolean isExpand,
            final View.OnClickListener onClickCallback
            ) {
        if (TextUtils.isEmpty(originText)) {
            return;
        }
        textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (isExpand) {
                    textView.setText(originText);
                } else {
                    int paddingLeft = textView.getPaddingLeft();
                    int paddingRight = textView.getPaddingRight();
                    TextPaint paint = textView.getPaint();
                    float moreText = paint.measureText("..." + endText);
                    float availableTextWidth = (textView.getWidth() - paddingLeft - paddingRight) * maxLines - moreText;
                    CharSequence ellipsizeStr = TextUtils.ellipsize(originText, paint, availableTextWidth, TextUtils.TruncateAt.END);
                    if (ellipsizeStr.length() < originText.length()) {
                        SpanUtils.with(textView)
                                .append(ellipsizeStr)
                                .append(endText).setForegroundColor(Color.parseColor("#333333"))
                                .setBackgroundColor(Color.parseColor("#0a000000"))
                                .setClickSpan(new ClickableSpan() {
                                    @Override
                                    public void onClick(@NonNull View widget) {
                                        onClickCallback.onClick(widget);
                                    }

                                    @Override
                                    public void updateDrawState(@NonNull TextPaint ds) {
                                        ds.setColor(Color.parseColor(endColor));
                                        ds.setUnderlineText(false);
                                    }
                                })
                                .create();
                    } else {
                        textView.setText(originText);
                    }
                }
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    public static void setUpdateEllipsize(
            final TextView textView,
            final CharSequence originText,
            final float endBtnWidth,
            final boolean isExpand,
            Function1<Boolean, Unit> onResult
    ) {
        if (TextUtils.isEmpty(originText)) {
            return;
        }
        textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                    int paddingLeft = textView.getPaddingLeft();
                    int paddingRight = textView.getPaddingRight();
                    TextPaint paint = textView.getPaint();
                    float moreText = paint.measureText("...") + endBtnWidth;
                    float availableTextWidth = (textView.getWidth() - paddingLeft - paddingRight) * 2 - moreText;
                    CharSequence ellipsizeStr = TextUtils.ellipsize(originText.toString(), paint, availableTextWidth, TextUtils.TruncateAt.END);
                    int resultLength = ellipsizeStr.length();
                    try {
                        if(originText.toString().contains("\n")){
                            float availableTextWidthForLine = (textView.getWidth() - paddingLeft - paddingRight);
                            float availableTextWidthForLineWithEnd = (textView.getWidth() - paddingLeft - paddingRight) - moreText;
                            String[] lines = originText.toString().split("\n");
                            if(lines.length >= 2){
                                CharSequence lineOneEllipsizeStr = TextUtils.ellipsize(lines[0], paint, availableTextWidthForLine, TextUtils.TruncateAt.END);
                                if(lineOneEllipsizeStr.length() < lines[0].length()){
                                    //超过一行
                                    CharSequence lineOneSecondEllipsizeStr = TextUtils.ellipsize(lines[0], paint, availableTextWidth, TextUtils.TruncateAt.END);
                                    if(lineOneSecondEllipsizeStr.length() < lines[0].length()){
                                        //超过两行
                                        ellipsizeStr = lineOneSecondEllipsizeStr;
                                        resultLength = ellipsizeStr.length();
                                    } else {
                                        ellipsizeStr = lines[0] + "...";
                                        resultLength = lines[0].length();
                                    }
                                } else {
                                    CharSequence lineTwoEllipsizeStr = TextUtils.ellipsize(lines[1], paint, availableTextWidthForLineWithEnd, TextUtils.TruncateAt.END);
                                    if(lineTwoEllipsizeStr.length() < lines[1].length()){
                                        //超过一行
                                        ellipsizeStr = lines[0] +"\n"+ lineTwoEllipsizeStr;
                                        resultLength = ellipsizeStr.length();
                                    } else {
                                        if(lines.length > 2){
                                            ellipsizeStr = lines[0] +"\n"+ lineTwoEllipsizeStr + "...";
                                            resultLength = (lines[0] +"\n"+ lineTwoEllipsizeStr).length();
                                        } else {
                                            ellipsizeStr = lines[0] +"\n"+ lineTwoEllipsizeStr;
                                            resultLength = ellipsizeStr.length();
                                        }
                                    }
                                }
                            }
                        }
                    }catch (Exception e){ }

                    if (resultLength < originText.length()) {
                        onResult.invoke(true);
                        if(isExpand){
                            textView.setText(originText);
                        } else {
                            textView.setText(ellipsizeStr);
                        }
                    } else {
                        onResult.invoke(false);
                        textView.setText(originText);
                    }
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

}
